﻿using AlarmModule.DbContexts;
using AlarmModule.Enums;
using AlarmModule.Interface.Implementation;
using AlarmModule.Models;
using AlarmModule.Utils;
using LogModule.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AlarmModule.Views {
    /// <summary>
    /// AlarmView.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmView : UserControl {
        //数据库上下文
        private readonly AlarmDbContext _dbContext;
        private readonly ILogService _logService;
        private Dictionary<string, AlarmParameter> parameters;
        private AlarmEngine engine;
        private AlarmCenter alarmCenter;
        public AlarmView(ILogService logService) {
            _logService = logService;
            InitializeComponent();
            //// 1. 初始化报警参数
            //parameters = new Dictionary<string, AlarmParameter> {
            //    ["Temperature"] = new AlarmParameter {
            //        TagName = "反应釜温度",
            //        LowLimit = 50,
            //        HighLimit = 80,
            //        LowLowLimit = 40,
            //        HighHighLimit = 90,
            //        Setpoint = 65,
            //        MaxDeviation = 10,
            //        Hysteresis = 2,
            //        TriggerDelayMs = 3000 // 3秒延时
            //    }
            //};


            //// 2. 创建报警引擎与中心
            //engine = new AlarmEngine(parameters);//报警引擎,将评判标准给报警引擎
            //alarmCenter = new AlarmCenter();//报警中心
            //alarmCenter.Subscribe(new LogAlarmNotifier(_logService, _dbContext));//报警中心添加订阅者,订阅者有报警处理器
            ////alarmCenter.Subscribe(new SmsAlarmNotifier()); // 短信通知实现类似


        }

        //private void Button_Click(object sender, RoutedEventArgs e) {
        //    Task.Run(() => {
        //        // 3. 模拟数据输入
        //        double[] testValues = { 99, 110, 100, 96, 60, 95 };
        //        foreach (var value in testValues) {
        //            var level = engine.CheckAlarm("Temperature", value);//报警引擎根据评判标准给出报警水平
        //            if (level != AlarmLevel.Normal) {//报警水平不正常,报警中心遍历所有订阅者处理警报
        //                alarmCenter.TriggerAlarm("Temperature", level);
        //                Console.WriteLine($"[报警] {parameters["Temperature"].TagName} = {value} → {level}");
        //            }
        //            //Thread.Sleep(1000); // 模拟1秒采集周期
        //            Task.Delay(1000);
        //        }
        //    });
           

        //}
    }
}
